package com.shisfish.library.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import com.shisfish.library.Impl.MediaImpl;
import com.shisfish.library.Model.Media;
import com.shisfish.library.Prere.Login;
import com.shisfish.library.Utils.Global.GlobalVariable;
import com.shisfish.library.Utils.Global.toJsonUtils;
import com.shisfish.library.Utils.PageModel.Page;
import com.shisfish.library.Utils.PageModel.PageUtils;

@Path("media")
public class MediaController extends GlobalVariable {

	private MediaImpl mediaImpl = new MediaImpl();
	
	@POST
	@Path("insertAMedia/{idadmin}")
	@Produces("application/json; charset=utf-8")
	public String insertAMedia(@PathParam("idadmin") String idadmin, @FormParam("name") String name, 
			@FormParam("address") String address, @FormParam("summary") String summary, 
			@FormParam("type") int type, @FormParam("pic") String pic, 
			@Context HttpServletRequest request) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			Media media = new Media(name, address, summary, type, pic);
			
			int flag = mediaImpl.insertOne(media);
			if (flag == 1) {
				globalMap.put(STATUS_CODE, SUCCESS);
			} else {
				globalMap.put(STATUS_CODE, INSERT_WR);
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("updateAMedia/{idadmin}/{id}")
	@Produces("application/json; charset=utf-8")
	public String updateAMedia(@PathParam("idadmin") String idadmin, @FormParam("name") String name, 
			@FormParam("address") String address, @FormParam("summary") String summary, 
			@FormParam("type") int type, @FormParam("pic") String pic, 
			@Context HttpServletRequest request, @PathParam("id") int id) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			
			List<Media> medias = mediaImpl.selectById(id);
			if (medias.size() > 0) {
				Media media = medias.get(0);
				media.setAddress(address);
				media.setName(name);
				media.setPic(pic);
				media.setSummary(summary);
				media.setType(type);
				
				int flag = mediaImpl.updateOne(media);
				if (flag == 0) {
					globalMap.put(STATUS_CODE, SELECT_WR);
				} else {
					globalMap.put(STATUS_CODE, SUCCESS);
				}
			} else if (medias.size() == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			} else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}
			
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("deleteAMedia/{idadmin}/{id}")
	@Produces("application/json; charset=utf-8")
	public String deleteAMedia(@PathParam("idadmin") String idadmin,  
			@Context HttpServletRequest request, @PathParam("id") int id) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			
			int flag = mediaImpl.deleteOneById(id);
			if (flag == 0) {
				globalMap.put(STATUS_CODE, INSERT_WR);
			} else {
				globalMap.put(STATUS_CODE, SUCCESS);
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectByPage/{type}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectByPage(@PathParam("type") int type, @PathParam("currentPage") int currentPage, @PathParam("pageSize") int pageSize) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		int count = mediaImpl.countByType(type);
		
		if (count > 0) {
			
			Page page = PageUtils.createPage(count, currentPage, pageSize);
			List<Map<String, Object>> medias = mediaImpl.selectByTypePage(type, page.getBeginIndex(), pageSize);
			
			if (medias.size() > 0) {
				globalMap.put("list", medias);
				globalMap.put("page", page);
				globalMap.put(STATUS_CODE, SUCCESS);
			} else if (medias.size() == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			} else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}
			
		} else if (count == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		} else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectById/{id}")
	@Produces("application/json; charset=utf-8")
	public String selectById(@PathParam("id") int id) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		Map<String, Object> media = mediaImpl.selectId(id);
		
		if (media != null) {
			globalMap.put(STATUS_CODE, SUCCESS);
			globalMap.put("obj", media);
		} else {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectByFcode/{fcode}")
	@Produces("application/json; charset=utf-8")
	public String selectByFcode(@PathParam("fcode") int fcode) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		List<Map<String, Object>> medias = mediaImpl.selectByFcode(fcode);
		
		if (medias.size() > 0) {
			globalMap.put(STATUS_CODE, SUCCESS);
			globalMap.put("list", medias);
		} else {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("selectByCho/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectByCho(@PathParam("currentPage") int currentPage, @PathParam("pageSize") int pageSize, 
			@FormParam("cho") String cho) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		int count = mediaImpl.countByCho(cho);
		if (count > 0) {
			Page page = PageUtils.createPage(count, currentPage, pageSize);
			List<Map<String, Object>> medias = mediaImpl.selectByChoPage(cho, currentPage, pageSize);
			if (medias.size() > 0) {
				globalMap.put("list", medias);
				globalMap.put("page", page);
				globalMap.put(STATUS_CODE, SUCCESS);
			} else {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			}
		} else {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
}
